package com.dgz.springboot18.controller;

import com.dgz.springboot18.mapper.StudentMapper;
import com.dgz.springboot18.mapper.StudentTaskMapper;
import com.dgz.springboot18.mapper.TaskMapper;
import com.dgz.springboot18.pojo.StudentInfo;
import com.dgz.springboot18.pojo.StudentTask;
import com.dgz.springboot18.pojo.TaskInfo;
import com.dgz.springboot18.pojo.vo.StuAndTask;
import com.dgz.springboot18.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

     private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

     @Autowired
     private StudentTaskMapper studentTaskMapper;
    /**
     * 学生登陆
     * @param studentInfo
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Map<String, Object> login(StudentInfo studentInfo, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        StudentInfo result = studentService.queryStu(studentInfo);
        if (result != null) {
            session.setAttribute("student", result);
            map.put("code", 0);
            map.put("msg", "登陆成功");
            map.put("data", "result");
        } else {
            map.put("code", 100);
            map.put("msg", "账户不存在");
            map.put("data", "");
        }
        return map;
    }

    /**
     * 全查询学生打卡信息
     * @param session
     * @return
     */
    @GetMapping("/signTask")
    public Map<String,Object> querySignTask(HttpSession session){
        Map<String, Object> map = new HashMap<>();
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        List<StuAndTask> stuAndTasks = studentService.querySignTask(student.getStuName());
        map.put("code",0);
        map.put("data",stuAndTasks);
        return map;
    }

    /**
     * 学生打卡操作
     */
    @PostMapping("/sign")
    public Map<String,Object> sign(HttpSession session,String taskId,String taskAndTime) throws ParseException {
        StudentInfo student = (StudentInfo) session.getAttribute("student");
        Map<String, Object> map = new HashMap<>();
         Date parse = sdf.parse(taskAndTime);
         if (new Date().getTime()>parse.getTime()){
             map.put("code",100);
             map.put("msg","打卡失败,任务已经结束");
         }else {
             StudentTask studentTask=new StudentTask();
             studentTask.setTaskId(taskId);
             studentTask.setStuId(student.getStuId());
             studentTask.setFlagTask(true);
             studentTask.setIsTime(LocalDateTime.now());
             System.out.println(studentTask);
             studentTaskMapper.save(studentTask);
             map.put("code",00);
             map.put("msg","打卡成功");
         }
        return map;
    }



}
